import numpy as np

# 读取数据
file_path = input("请选择坐标文件：")
data = np.loadtxt(file_path, dtype=str, delimiter=',')

file_param = data[:, 0]  # 保存文件参数
file_name = data[:, 1]  # 保存文件名
y_coord = data[:, 2].astype(float)  # 保存y坐标
x_coord = data[:, 3].astype(float)  # 保存x坐标

# 计算两点之间距离
delta_x = np.zeros((9, 9))
delta_y = np.zeros((9, 9))
distance = np.zeros((9, 9))
observed_distance = np.zeros((9, 9))

for i in range(9):
    for j in range(9):
        delta_x[i, j] = x_coord[j] - x_coord[i]
        delta_y[i, j] = y_coord[j] - y_coord[i]
        distance[i, j] = np.sqrt(delta_x[i, j] ** 2 + delta_y[i, j] ** 2)  # 计算距离
        errors = np.random.normal(0, (3.67 + 2 * distance[i, j] / 1000))
        observed_distance[i, j] = distance[i, j] + errors / 1000  # 距离观测值随机误差后的距离观测值

# 计算坐标方位角
angle = np.zeros((9, 9))
int_angle = np.zeros((9, 9), dtype=int)
minute_angle = np.zeros((9, 9), dtype=int)
second_angle = np.zeros((9, 9))

for i in range(9):
    for j in range(9):
        if delta_x[i, j] == 0 and delta_y[i, j] > 0:
            angle[i, j] = 90
        else:
            R = np.arctan2(delta_y[i, j], delta_x[i, j])
            K = np.degrees(R)
            angle[i, j] = (K + 360) % 360

        if i == j:
            angle[i, j] = 0

int_angle = np.floor(angle)
minute_angle = np.floor((angle - int_angle) * 60)
second_angle = ((angle - int_angle) * 60 - minute_angle) * 60
azimuth = int_angle + minute_angle / 100 + second_angle / 10000

# 方位角
random_value = np.zeros((9, 9))

for i in range(9):
    for j in range(9):
        if file_name[i] == 'QZ1':
            l = azimuth[i, j] - azimuth[i, 2]
            if l < 0:
                l = l + 360
        else:
            l = azimuth[i, j] - azimuth[i, 1]
            if l < 0:
                l = l + 360
        random_value[i, j] = np.random.normal(0, 1) * (83 / 10000 + l)

# 对话框大小
dialog_size = [1, 50]

# 输入先验精度
precision_info = input("输入先验精度（方向观测值中误差、测距固定误差、比例误差）：")
direction_error, fixed_error, ratio_error = map(float, precision_info.split(","))

# 输入起算数据
init_data = input("输入起算数据（起算点名、起算点Y坐标、起算点X坐标、起算边终点、起算坐标方位角）：")
start_point_name, start_y, start_x, end_point_name, start_azimuth = init_data.split(",")
start_y = float(start_y)
start_x = float(start_x)
start_azimuth = float(start_azimuth)

with open('./wkr.txt', 'w') as file:
    file.write(f"{direction_error},{fixed_error},{ratio_error}\n")
    file.write(f"{file_name[0]},{y_coord[0]},{x_coord[0]}\n")

    for i in range(9):
        file.write(f"{file_name[i]}\n")
        for j in range(9):
            if i != j:
                file.write(f"{file_name[j]},L,{random_value[i, j]}\n")

        for j in range(9):
            if i != j:
                file.write(f"{file_name[j]},S,{observed_distance[i, j]}\n")

print("程序完成，输出wkr.txt文件")
